import redisclass
import mongoclass


class Logic:
    def __init__(self):
        self.redis_client = redisclass.Redis()
        self.mongo_client = mongoclass.Mongo()

    # 拆档
    def demolition_a(self, acvs_id):
        # 查看是否是p
        acvs_id_img = self.mongo_client.get_acvs_img(acvs_id)
        if acvs_id_img.get("p") is None:
            raise Exception("acvs is p")
        else:
            acvs_p_id = acvs_id_img.get("p")
            # 建表
            self.redis_client.rdb_demolition_acvs(acvs_id, acvs_p_id)
            # db2
            corr_list = self.redis_client.rdb_acvs_acvs(acvs_id, acvs_p_id)
            print(corr_list)
            self.mongo_client.mdb_demolition_acvs(acvs_id, acvs_p_id, corr_list)
        pass

    # 拆脸
    def demolition_f(self, acvs_id, face_id):
        if self.redis_client.rdb_get_face_id(face_id, acvs_id):
            raise Exception("face is build_acvs_face")

        self.redis_client.rdb_demolition_face(acvs_id, face_id)
        self.redis_client.rdb_del_face_acvs(face_id, acvs_id)
        self.redis_client.rdb_del_acvs_face(face_id, acvs_id)
        self.mongo_client.mdb_demolition_face(acvs_id, face_id)
        pass

    # 合档
    def merge_a(self, acvs_id, acvs_p_id):
        # 先拆
        acvs_id_img = self.mongo_client.get_acvs_img(acvs_id)
        if acvs_id_img.get("p") is not None:
            if acvs_id_img.get("p") == acvs_p_id:
                raise Exception("acvs_p_id Already acvs_id p")
            self.demolition_a(acvs_id)
        else:
            self.redis_client.rdb_mer_acvs(acvs_id, acvs_p_id)
            acvs_p_img = self.mongo_client.get_acvs_img(acvs_p_id)
            if acvs_p_img['len1'] + acvs_p_img['len2'] >= acvs_id_img['len1'] + acvs_id_img['len2']:
                # 正常
                self.mongo_client.mdb_merge_acvs(acvs_id, acvs_p_id)

            else:
                # 参数相反
                print("反向合并")
                self.mongo_client.mdb_merge_acvs(acvs_p_id, acvs_id)
        pass

    # 合脸
    def merge_f(self, acvs_id, face_id):
        face_all_img = self.redis_client.get_face_img(face_id)
        self.redis_client.rdb_mer_face(face_id, acvs_id)
        if self.redis_client.rdb_get_face_acvs(face_id, acvs_id):
            raise Exception("face Aready in acvs")
        else:
            if face_all_img.get("is_front") == "False":
                level = 1100001
                # is_front = False
            elif face_all_img.get("is_front") == "True":
                level = 100001
                # is_front = True
            else:
                raise Exception("face_id error")

            self.redis_client.rdb_zadd_face_acvs(face_id, acvs_id, level)
            self.redis_client.reb_zadd_acvs_face(acvs_id, face_id, level)

            self.mongo_client.mdb_mer_face(face_id, acvs_id, face_all_img, level)